package servlet;

import dao.CardDao;
import entity.Card;
import org.apache.ibatis.session.SqlSession;
import service.CardService;
import service.impl.CardServiceImpl;
import utils.MybatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/CardMybatisServlet")
public class CardMybatisServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        String opr = "list";
        SqlSession sqlSession = MybatisUtil.createSqlSession();
        CardService service = new CardServiceImpl();

        if (req.getParameter("opr") != null){
            opr = req.getParameter("opr");
        }
        if (opr.equals("list")){

            int pageIndex = 1;
            if (req.getParameter("pageIndex") != null){
                pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
            }
            int pageSize = 3;
            int startIndex = (pageIndex - 1) * pageSize;

            int count = service.selectListSize();
            int pageCount = count / pageSize;
            if (count % pageSize > 0){
                pageCount += 1;
            }
            if (req.getParameter("pageIndex") != null)
                pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
            List<Card> cardList = service.selectList(startIndex,pageSize);

            req.setAttribute("list",cardList);
            req.setAttribute("pageIndex",pageIndex);
            req.setAttribute("pageCount",pageCount);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }else if (opr.equals("add")){
            String userName = req.getParameter("userName");
            String idNumber = req.getParameter("idNumber");
//            int status = Integer.parseInt(req.getParameter("status"));
            String createDate = req.getParameter("createDate");
            Card card = new Card(userName,idNumber,1,createDate);
            int r = service.add(card);
            if (r > 0) {
//                sqlSession.commit();
                out.print("<script>alert('添加成功！');location.href='CardMybatisServlet?opr=list';</script>");
            }else {
                sqlSession.rollback();
                out.print("<script>alert('添加失败！');location.href='CardMybatisServlet?opr=list';</script>");
            }
        }else if (opr.equals("update")){
            int status = Integer.parseInt(req.getParameter("status"));
            int id = Integer.parseInt(req.getParameter("id"));
            int r = service.update(id,status);
            String result = "修改失败";
            if (r > 0){
                result = "修改成功";
            }
            out.print(result);
        }else if (opr.equals("del")){
            int id = Integer.parseInt(req.getParameter("id"));
            int r = service.delete(id);
            String result = "删除失败";
            if (r > 0){
                result = "删除成功";
            }
            out.print(result);
        }
        MybatisUtil.closeSqlSession(sqlSession);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
